package com.ruoyi.warning.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.warning.domain.entity.AlertRules;
import com.ruoyi.warning.domain.model.Builder;
import com.ruoyi.warning.domain.model.Rule;
import com.ruoyi.warning.domain.query.AlertRulesQuery;
import com.ruoyi.warning.domain.vo.AlertRulesVO;
import com.ruoyi.warning.mapper.AlertRulesMapper;
import com.ruoyi.warning.service.IAlertRulesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-03-26
 */
@Service
public class AlertRulesServiceImpl extends ServiceImpl<AlertRulesMapper, AlertRules> implements IAlertRulesService {

    @Resource
    private AlertRulesMapper alertRulesMapper;
    @Override
    public TableDataInfo getPage(AlertRulesQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<AlertRulesVO> list = alertRulesMapper.getList(query);
        for(AlertRulesVO alertRule : list){
            if(alertRule.getBuilder()!=null){
                Builder builder = JSON.parseObject(alertRule.getBuilder(), Builder.class);
                String newBuilder = "";
                if(!builder.getValid()){
                    newBuilder = "!";
                }
                List<Rule> rules = builder.getRules();
                for (int i = 0; i < rules.size(); i++) {
                    Rule rule = rules.get(i);
                    switch (rule.getOperator()){
                        case "equal":
                            newBuilder+=rule.getField()+"="+rule.getValue();
                            break;
                        case "less":
                            newBuilder+=rule.getField()+"<"+rule.getValue();
                            break;
                        case "greater":
                            newBuilder+=rule.getField()+">"+rule.getValue();
                            break;
                        case "greater_or_equal":
                            newBuilder+=rule.getField()+">="+rule.getValue();
                            break;
                        case "less_or_equal":
                            newBuilder+=rule.getField()+"<="+rule.getValue();
                            break;
                        case "not_equal":
                            newBuilder+=rule.getField()+"!="+rule.getValue();
                            break;
                    }
                    if(i<rules.size()-1){
                        newBuilder+=" "+builder.getCondition()+" ";
                    }
                }
                alertRule.setBuilder(newBuilder);
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return tableDataInfo;
    }
}
